package com.muyi.dao.impl;

import com.muyi.pojo.entity.Account;
import com.muyi.pojo.entity.User;
import com.muyi.util.BaseDaoImpl;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements com.muyi.dao.UserDao {
    private BaseDaoImpl baseDao = new BaseDaoImpl();

    /**
     * 判断用户是否已经存在
     * @param nickname
     * @param gender
     * @param contact
     * @return
     * @throws Exception
     */
    @Override
    public boolean judgeUserExists(String nickname, String gender, String contact) throws Exception {
        String sql = "select * from user where nickname = ? and gender= ? and contact = ?";
        List<User> users = baseDao.queryMethod(sql, User.class, nickname, gender, contact);
        if (null != users && users.size() > 0){
            //说明用户已经存在，不能添加
            return false;
        }
        return true;
    }
    

    /**
     * 新增用户信息
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public int addUser(User user) throws Exception {
        String sql = "insert into user values(null,?,?,?,?,?,?,?,default,default,default)";
        int i = baseDao.cudMethod(sql, user.getNickname(), user.getGender(), user.getContact(), user.getBirthday(),
                user.getCareer_direction(), user.getGraduation_year(), user.getUser_password());
        return i;
    }

    @Override
    public int deleteUserById(Integer user_id) throws Exception {
        // 删除用户
        String sql = "delete from user where user_id = ?";
        return baseDao.cudMethod(sql, user_id);
    }

    @Override
    public int updateUser(User user) throws Exception {
        String sql = "update user set nickname= ?, gender= ?, contact=?, birthday = ?," +
                "career_direction=?, graduation_year=?, user_password=?, status=?, error_count=?, lock_time=default where user_id = ?";
        int i = baseDao.cudMethod(sql, user.getNickname(), user.getGender(), user.getContact(), user.getBirthday(),
                user.getCareer_direction(), user.getGraduation_year(), user.getUser_password(), user.getStatus(),
                user.getError_count(), user.getUser_id());
        return i;
    }

    @Override
    public int register(Account account) throws Exception {
        String sql = "insert into account values(null,?,?,?,now(),null)";
        int i = baseDao.cudMethod(sql, account.getUsername(), account.getPwd(), account.getImg_url());
        return i;
    }

    @Override
    public List<User> getUserBySearch(User user, Integer pageNo, Integer pageSize) throws Exception {
        //定义一个参数的集合
        ArrayList<Object> paramList = new ArrayList<>();
        String sql1 = "SELECT t1.user_id, t1.nickname, t1.gender, t1.contact, t1.birthday, t1.career_direction, t1.graduation_year, t1.user_password, t1.status, t1.error_count" +
                " FROM user as t1 " +
                " WHERE 1=1 ";
        if (null != user.getNickname() && !"".equals(user.getNickname())){
            sql1 += " and t1.nickname like ?";
            paramList.add("%"+user.getNickname()+"%");
        }
        if (null != user.getStatus() && !"".equals(user.getStatus())){
            sql1 += " and t1.contact like ?";
            paramList.add("%"+user.getStatus()+"%");
        }
        if (null != user.getGender() && !"".equals(user.getGender())){
            sql1 += " and t1.gender = ?";
            paramList.add(user.getGender());
        }
        if (null != user.getGraduation_year() && !"".equals(user.getGraduation_year())){
            sql1 += " and t1.graduation_year like ?";
            paramList.add("%"+user.getGraduation_year()+"%");
        }

        sql1 += " limit ?,?";
        paramList.add((pageNo-1)*pageSize);
        paramList.add(pageSize);

        //执行sql语句
        List<User> userList = baseDao.queryMethod(sql1, User.class, paramList.toArray());
        return userList;
    }

    @Override
    public long getTotal(User user) throws Exception {
        //定义一个参数的集合
        ArrayList<Object> paramList = new ArrayList<>();
        String sql1 = "SELECT t1.nickname, t1.gender, t1.contact, t1.birthday, t1.career_direction, t1.graduation_year, t1.user_password, t1.status " +
                " FROM user as t1 " +
                " WHERE 1=1 ";
        if (null != user.getNickname() && !"".equals(user.getNickname())){
            sql1 += " and t1.nickname like ?";
            paramList.add("%"+user.getNickname()+"%");
        }
        if (null != user.getStatus() && !"".equals(user.getStatus())){
            sql1 += " and t1.status like ?";
            paramList.add("%"+user.getStatus()+"%");
        }
        if (null != user.getGender() && !"".equals(user.getGender())){
            sql1 += " and t1.gender = ?";
            paramList.add(user.getGender());
        }
        if (null != user.getGraduation_year() && !"".equals(user.getGraduation_year())){
            sql1 += " and t1.graduation_year like ?";
            paramList.add("%"+user.getGraduation_year()+"%");
        }

        //执行sql语句
        List<User> userList = baseDao.queryMethod(sql1, User.class, paramList.toArray());
        return userList.size();
    }

    @Override
    public User getUserById(Integer user_id) throws Exception {
        String sql = "SELECT " +
                " user_id, nickname, gender, contact, birthday, career_direction, graduation_year, user_password, status" +
                ", error_count " +
                " FROM user " +
                " WHERE user_id = ? ";
        List<User> userList = baseDao.queryMethod(sql, User.class, user_id);
        if (userList != null && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public Account login(String username, String pwd) throws Exception {
        String sql = "select * from account where username = ? and pwd = ?";
        List<Account> accountList = baseDao.queryMethod(sql, Account.class, username, pwd);
        if (accountList != null && accountList.size() > 0) {
            return accountList.get(0);
        }
        return null;
    }

    @Override
    public int deleteUserByIds(Integer[] ids) throws Exception {
        // 动态生成 IN 子句
        StringBuilder placeholders = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            placeholders.append("?");
            if (i < ids.length - 1) {
                placeholders.append(", ");
            }
        }
        // 生成最终的 SQL 语句
        String sql = "delete from user where user_id in (" + placeholders.toString() + ")";

        // 使用 ids 数组作为参数调用 cudMethod 执行 SQL 删除操作
        return baseDao.cudMethod(sql, ids);
    }

}
